package com.jsonal.service.impl.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jsonal.common.CommonUtil;
import com.jsonal.entity.manager.Navicat;
import com.jsonal.entity.manager.NavicatBarBean;
import com.jsonal.mapper.NavicatMapper;
import com.jsonal.service.manager.NavicatService;
import com.jsonal.vo.Result;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: jsonal
 * @Description:
 * @Date: 2021/2/1 11:09
 * @Version: 1.0
 */
@Service
@Slf4j
public class NavicatServiceImpl implements NavicatService {

    @Autowired
    private NavicatMapper navicatMapper;

    @Override
    public Result getNavicatList(NavicatBarBean navicatBarBean) {
        List<Navicat> beans = new ArrayList<>();
        Result result = new Result();
        result.setSuccess(true);
        result.setMsg("获取数据成功");
        if (navicatBarBean == null) {
            // 不采取分页获取
            beans = navicatMapper.selectList(new QueryWrapper<Navicat>().eq("del_flag", false));
            if (beans != null && beans.size() > 0) {
                result.setResults(beans);
                return result;
            }
            result.setMsg("获取数据失败");
            result.setSuccess(false);
            return result;
        }
        IPage<Navicat> page = new Page<>(navicatBarBean.getPage(), navicatBarBean.getPageSize());
        IPage<Navicat> navicats = navicatMapper
            .selectPage(page, new QueryWrapper<Navicat>().eq("del_flag", false));
        if (navicats.getRecords().size() > 0) {
            for (Navicat record : navicats.getRecords()) {
                Navicat bean = new Navicat();
                BeanUtils.copyProperties(record, bean);
                beans.add(bean);
            }
            result.setResults(beans);
            result.setObj(navicats);
            return result;
        }
        result.setSuccess(false);
        result.setMsg("获取数据失败");
        return result;
    }

    /**
     * 添加或修改导航栏菜单
     * @param navicatBarBean form 表单传过来的数据
     * @return 成功或失败的结果
     */
    @Override
    public Result addOrUpdate(NavicatBarBean navicatBarBean) {
        Result result = new Result();
        Navicat navicat = new Navicat();
        if ((navicatBarBean.getId() != null) && !"".equals(navicatBarBean.getId())) {
            // id有值，说明是修改
            BeanUtils.copyProperties(navicatBarBean, navicat);
            navicat.setUpdateTime(new Date());
            int update = navicatMapper.updateById(navicat);
            if (update > 0) {
                result.setSuccess(true);
                result.setMsg("修改成功");
                return result;
            }
            result.setSuccess(false);
            result.setMsg("修改失败");
            return result;
        }
        // id 为空 为添加
        BeanUtils.copyProperties(navicatBarBean, navicat);
        navicat.setDelFlag(0);
        navicat.setId(CommonUtil.getGUID());
        navicat.setCreateTime(new Date());
        int insert = navicatMapper.insert(navicat);
        if (insert > 0) {
            result.setSuccess(true);
            result.setMsg("添加成功");
            return result;
        }
        result.setSuccess(false);
        result.setMsg("添加失败");
        return result;
    }

    /**
     * 根据 id 获取数据
     * @param id 唯一标识
     * @return 数据内容
     */
    @Override
    public Result getNavicatById(String id) {
        Result result = new Result();
        Navicat navicat = navicatMapper.selectOne(new QueryWrapper<Navicat>().eq("id", id).and(i -> i.eq("del_flag", false)));
        if (navicat!= null) {
            result.setObj(navicat);
            result.setMsg("获取数据成功");
            result.setSuccess(true);
            return result;
        }
        result.setSuccess(false);
        result.setMsg("id无效");
        return result;
    }

    /**
     * 根据 id 删除数据（逻辑删除）
     * @param id 唯一标识
     * @return 返回成功或失败
     */
    @Override
    public Result deleteById(String id) {
        Result result = new Result();
        Navicat navicat = navicatMapper.selectOne(new QueryWrapper<Navicat>().eq("id", id).and(i -> i.eq("del_flag", false)));
        if (navicat != null) {
            navicat.setDelFlag(1);
            int update = navicatMapper.updateById(navicat);
            if (update > 0) {
                result.setMsg("删除成功");
                result.setSuccess(true);
                return result;
            }
            result.setMsg("删除失败");
            result.setSuccess(false);
            return result;
        }
        result.setSuccess(false);
        result.setMsg("id无效");
        return result;
    }
}
